﻿using ICSharpCode.AvalonEdit;
using ICSharpCode.AvalonEdit.CodeCompletion;
using JVMTI_CONN_CONNECT;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Shell;

namespace JVMTI_CORE_SHELL
{
    /// <summary>
    /// Interaction logic for CodeViewer.xaml
    /// </summary>
    public partial class CodeViewer : Window
    {

        public CodeViewer()
        {
            InitializeComponent();
            var Resizable_BorderLess_Chrome = new WindowChrome();
            Resizable_BorderLess_Chrome.GlassFrameThickness = new Thickness(0);
            Resizable_BorderLess_Chrome.CornerRadius = new CornerRadius(0);
            Resizable_BorderLess_Chrome.CaptionHeight = 5.0;
            WindowChrome.SetWindowChrome(this, Resizable_BorderLess_Chrome);
            this.Closed += CodeViewer_Closed;

        }

        CompletionWindow completionWindow;

        //void textEditor_TextArea_TextEntered(object sender, TextCompositionEventArgs e)
        //{
        //    if (e.Text == ".")
        //    {
        //        // Open code completion after the user has pressed dot:
        //        completionWindow = new CompletionWindow(textEditor.TextArea);
        //        IList<ICompletionData> data = completionWindow.CompletionList.CompletionData;
        //        completionWindow.Show();
        //        completionWindow.Closed += delegate
        //        {
        //            completionWindow = null;
        //        };
        //    }
        //}

        //void textEditor_TextArea_TextEntering(object sender, TextCompositionEventArgs e)
        //{
        //    if (e.Text.Length > 0 && completionWindow != null)
        //    {
        //        if (!char.IsLetterOrDigit(e.Text[0]))
        //        {
        //            // Whenever a non-letter is typed while the completion window is open,
        //            // insert the currently selected element.
        //            completionWindow.CompletionList.RequestInsertion(e);
        //        }
        //    }
        //    // Do not set e.Handled=true.
        //    // We still want to insert the character that was typed.
        //}

        void CodeViewer_Closed(object sender, EventArgs e)
        {
            App.CodeViewerWindow = null;
        }

        private void Window_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            this.DragMove();
        }

        public void ScrollToLine(int lineno)
        {
            ((EditorTabs.Items[EditorTabs.Items.Count - 1] as TabItem).Content as TextEditor).ScrollToLine(lineno);
        }

        private void closeTab_Click(object sender, RoutedEventArgs e)
        {
            if (EditorTabs.Items.Count > 1)
                EditorTabs.Items.RemoveAt(EditorTabs.SelectedIndex);
            else
            {
                this.Close();
                App.CodeViewerWindow = null;
            }
        }

        private void navigateTab_Click(object sender, RoutedEventArgs e)
        {
            int idx = EditorTabs.SelectedIndex;
            try
            {
                StackTraceItem item = (EditorTabs.Items[idx] as TabItem).DataContext as StackTraceItem;
                JVMTI_CORE_SHELL.SHOpenFolderAndSelectItems.ShowSelectedInExplorer.FilesOrFolders(
                   App.searchForFiles(item.FileName)
                );
            }
            catch { }
        }
    }
}
